/*
 * Copyright (c) 2011, 2012 Alexander Kluth <alexander.kluth@outlook.com>
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package hardyhttp.server;

import hardyhttp.filesystem.Logger;
import java.io.*;
import java.net.*;
import com.sun.net.httpserver.*;


/**
 *
 * @author akluth
 */
public class HttpStatusCodes 
{
    Logger logger;
    HttpExchange exchange;
    
    public HttpStatusCodes()
    {

    }
    
    
    /**
     * set-method for overgiving the HttpExchange-variable from
     * CommonHandler.java
     * 
     * @param exchange 
     */
    public void setHttpExchange(HttpExchange exchange)
    {
        this.exchange = exchange;
    }
    
    
    /**
     * set-method for overgiving the Logger-variable from
     * CommonHandler.java
     * 
     * @param logger 
     */
    public void setLogger(Logger logger)
    {
        this.logger = logger;
    }
    
    
    /**
     * Sends a 200 to the client when everything went OK
     * 
     * @param length Length of the response
     * @return true if 200 was sent, false otherwise
     */
    public boolean send200(int length)
    {
        try
        {
            logger.logEvent("[HTTPD]  Sent HTTP 200 OK to " + exchange.getRemoteAddress().toString());
            exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, length);
            return true;
        }
        
        catch (IOException ion)
        {
            logger.logEvent("[HTTPD]  ERROR: Could not send HTTP 200 to the client!");
            return false;
        }
    }
    
    
    /**
     * Sends a 404 to the client if the specified file is not found
     * 
     * @return true if 404 could be sent, false otherwise
     */
    public boolean send404()
    {
        try 
        {
            logger.logEvent("[HTTPD]  WARNING: Sent HTTP 404 to " + exchange.getRemoteAddress().toString());
            exchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_FOUND, 0);
            return true;
        } 
                
        catch (IOException ex) 
        {
            logger.logEvent("[HTTPD]  ERROR: Could not send HTTP 404 to the client!");
            return false;
        }
    }
    
    
    /**
     * Sends a 500 to the client if everything is fucked up in the server
     * 
     * @return true if 500 could be sent, false otherwise (so everything seems to be REAL fucked up)
     */
    public boolean send500()
    {        
        try
        {
            exchange.sendResponseHeaders(HttpURLConnection.HTTP_INTERNAL_ERROR, 0);
            logger.logEvent("[HTTPD]  WARNING: Sent HTTP 500 to client " + exchange.getRemoteAddress().toString());
            
            return true;
        }
                    
        catch (IOException ioeII)
        {
            logger.logEvent("[HTTPD]  CRITICAL: Could not even send HTTP 500 to the client! Stopping");
            return false;
        }
    }
}
